package t327.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import t327.pojo.*;
import t327.pojo.font.LuiTableEntity;
import t327.pojo.until.Result;
import t327.pojo.views.ContractOrderClientView;
import t327.pojo.views.OrderColorSizeClientView;
import t327.pojo.views.ProductionPlanOrderSizeDepartmentView;
import t327.service.iter.IClientService;
import t327.service.iter.IContractService;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("contract")
public class ContractController {
    @Autowired
    private IContractService contractService;
    @Autowired
    private IClientService clientService;
    @RequestMapping("getContract")
    public List<Contract> getContract(){
        return contractService.queryContract();
    }
    @RequestMapping("addContract")
    public Result addContract(@RequestBody Contract contract){
       // List<OrderForm> orderForms = contractService.queryOrder();
        String contractNumber = contract.getContractNumber();
        Long clientId = contract.getClientId();
        Long orderId = contract.getOrderId();
        Date contractTime = contract.getContractTime();
        String supplier = contract.getSupplier();

        String deliveryAddress1 = contract.getDeliveryAddress();
        Date deliveryTime = contract.getDeliveryTime();
        BigDecimal orderMoney = contract.getOrderMoney();
        /*if(!deliveryAddress.equals(deliveryAddress1)){
            return new Result(205,"您输入的交货地址不不符",false,null);
        }
        if(orderMoney!=totalPrice){
            return new Result(206,"您输入的价格与订单不符",false,null);
        }
        if(!deliveryTime.equals(deliveryTime1)){
            return new Result(209,"您输入的日期与订单不符",false,null);
        }*/
        if (!contractNumber.equals("")&&clientId>0&&orderId>0&&!deliveryAddress1.equals("")&&!supplier.equals("")&&contractTime!=null&&deliveryTime!=null||orderMoney.compareTo(orderMoney.ZERO)>0){
            if (!isNumeric(orderMoney.toString())){
                return new Result(203,"订单价格只能是数字",false,null);
            }
            if (contractTime.after(deliveryTime)){
                return new Result(204,"签约时间不能在订单时间之后",false,null);
            }
            int i = contractService.addContract(contract);
            if (i>0){
                return Result.success();
            }else {
                return new Result(201,"合同保存失败",false,null);
            }
        }
        return Result.error();

    }
    //验证是单价和总价是否是数字
    public boolean isNumeric(String input) {
        try {
            Long.parseLong(input);  // 尝试将输入解析为整数
            // 或
            // Double.parseDouble(input);  // 尝试将输入解析为浮点数
            return true;  // 解析成功，输入为数字
        } catch (NumberFormatException e) {
            return false;  // 解析失败，输入不是数字
        }
    }
    //验证时间是否正确
    public  boolean isValidDate(String dateString, String pattern) {
        try {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
            LocalDate.parse(dateString, formatter);
            return true;  // 日期格式正确
        } catch (DateTimeParseException e) {
            return false;  // 日期格式错误
        }
    }
    @RequestMapping("quertyContractOrderClient")
    public LuiTableEntity quertyContractOrderClient(@RequestParam(value = "page") Integer pageNum, @RequestParam(value = "limit")Integer pageSize){
/*
*     "code": res.status, //解析接口状态
      "msg": res.message, //解析提示文本
      "count": res.total, //解析数据长度
      "data": res.data.item //解析数据列表
* */
        PageHelper.startPage(pageNum,pageSize);
        LuiTableEntity luiTableEntity = new LuiTableEntity<OrderColorSizeClientView>();
        PageInfo<ContractOrderClientView> contractOrderClientViewPageInfo = new PageInfo<>(contractService.quertyContractOrderClient());
        List<ContractOrderClientView> contractOrderClient = contractOrderClientViewPageInfo.getList();
        long total = contractOrderClientViewPageInfo.getTotal();
        if(contractOrderClient!=null&&contractOrderClient.size()>0){
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(contractOrderClient);
        }else{
            luiTableEntity.setCode("202");
            luiTableEntity.setMsg("查询失败");
        }
        return   luiTableEntity;
    }
    @RequestMapping("queryOrder")
    public  List<OrderForm> queryOrder(){
        return contractService.queryOrder();
    }
    @RequestMapping("queryDepartment")
    public  List<Department> queryDepartment(){
        return contractService.queryDepartment();
    }
    @RequestMapping("getroductionPlan")
    public ProductionPlan getroductionPlan(Long productionPlanId){
        return contractService.queryProductionPlan(productionPlanId);
    }
    @RequestMapping("queryColorMaterials")
    public  List<ColorMaterialsRel> queryColorMaterials(){
        return contractService.queryColorMaterials();
    }

    @RequestMapping("addcuttingDepartment")
    public  Result addcuttingDepartment(@RequestBody Submission submission){
        Long departmentId = submission.getDepartmentId();
        Long ocsrId = submission.getOrderId();
        Long producedCount = submission.getProducedCount();
        Long productionPlanId = submission.getProductionPlanId();
        HashMap<String, String> map = contractService.addSubmission(submission);
        String code = map.get("code");
        if (departmentId<=0L || ocsrId<=0L || producedCount<=0L || productionPlanId<=0L){
            return new Result(221,"请填写全部，不能有空哦",false,null);
        }else {
            if (!isNumeric(producedCount.toString())){
                return new Result(222,"请填写数字",false,null);
            }
            if (code.equals("200")){
                return new Result(200,map.get("msg"),false,null);
            }
            return new Result(230,"保存失败",false,null);
        }
    }

    @RequestMapping("queryProductionPlan")
    public LuiTableEntity queryProductionPlan(@RequestParam(value = "page") Integer pageNum, @RequestParam(value = "limit")Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        LuiTableEntity luiTableEntity = new LuiTableEntity<OrderColorSizeClientView>();
        PageInfo<ProductionPlanOrderSizeDepartmentView> planOrderSizeDepartmentViewPageInfo = new PageInfo<>(contractService.queryAll());
        List<ProductionPlanOrderSizeDepartmentView> planOrderSizeDepartmentClient = planOrderSizeDepartmentViewPageInfo.getList();
        long total = planOrderSizeDepartmentViewPageInfo.getTotal();
        if(planOrderSizeDepartmentClient!=null&&planOrderSizeDepartmentClient.size()>0){
            luiTableEntity.setCode("0");
            luiTableEntity.setMsg("查询成功");
            luiTableEntity.setCount(total);
            luiTableEntity.setData(planOrderSizeDepartmentClient);
        }else{

            luiTableEntity.setCode("202");
            luiTableEntity.setMsg("查询失败");
        }
        return   luiTableEntity;
    }
    @RequestMapping("updateContract")
    public Result updateContract(String contractNumber){
        int i = contractService.updateContract(contractNumber);
        if (i > 0) {
            return Result.success();
        }
        return new Result(404,"提交失败",false,null);
    }
    @RequestMapping("getclient")
    public List<Client> getclient(){
        return clientService.queryAll();
    }
    @RequestMapping("updateContractPay")
    public Result updateContractPay(Long contractId){
        int i = contractService.updateContractPay(contractId);
        if (i > 0) {
            return Result.success();
        }
        return new Result(404,"提交失败",false,null);
    }
}
